home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 02 Useful Techniques / 08 Zarozinski / src / ffllapi / FuzzyOutSet.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-01  |  5.7 KB  |  269 lines

  1.  
  2. //
  3. // File:    FuzzyOutSet.cpp
  4. //
  5. // Purpose:    This file contains  code for the FuzzyOutSet class
  6. //
  7. // Copyright ⌐ 1999-2001 Louder Than A Bomb! Software
  8. //
  9. // This file is part of the FFLL (Free Fuzzy Logic Library) project (http://ffll.sourceforge.net)
  10. // It is released under the BSD license, see http://ffll.sourceforge.net/license.txt for the full text.
  11. // 
  12.  
  13. #include "FuzzyOutSet.h"
  14. #include "COGDefuzzSetObj.h"
  15. #include "MOMDefuzzSetObj.h"
  16. #include "FuzzyOutVariable.h"
  17.  
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. //
  25. // Function:    FuzzyOutSet()
  26. // 
  27. // Purpose:        FuzzyOutSet constructor
  28. //
  29. // Arguments:
  30. //
  31. //        FuzzyVariableBase* par - parent of this set
  32. //
  33. // Returns:
  34. //
  35. //        nothing
  36. //
  37. // Author:    Michael Zarozinski
  38. // Date:    8/01
  39. // 
  40. // Modification History
  41. // Author    Date        Modification
  42. // ------    ----        ------------
  43. //
  44. //
  45. FuzzyOutSet::FuzzyOutSet(FuzzyVariableBase* par) : FuzzySetBase(par), FFLLBase(par) 
  46. {
  47.     defuzz_obj = NULL; 
  48.  
  49.     //  default defuzzification method to the parent's method
  50.     int type = (dynamic_cast<FuzzyOutVariable*>(par))->get_defuzz_method();
  51.  
  52.     set_defuzz_method(type);
  53.  
  54. }; // end FuzzyOutSet::FuzzyOutSet()
  55.  
  56. //
  57. // Function:    ~FuzzyOutSet()
  58. // 
  59. // Purpose:        FuzzyOutSet destructor
  60. //
  61. // Arguments:
  62. //
  63. //        FuzzyVariableBase* par - parent of this set
  64. //
  65. // Returns:
  66. //
  67. //        nothing
  68. //
  69. // Author:    Michael Zarozinski
  70. // Date:    8/01
  71. // 
  72. // Modification History
  73. // Author    Date        Modification
  74. // ------    ----        ------------
  75. //
  76. //
  77. FuzzyOutSet::~FuzzyOutSet()
  78. {
  79.     if (defuzz_obj)
  80.         delete defuzz_obj;
  81.  
  82. }; // end FuzzyOutSet::~FuzzyOutSet()
  83.  
  84. //
  85. // Function:    set_defuzz_method()
  86. // 
  87. // Purpose:        Set the defuzzification method for this set. This involves
  88. //                creating the correct DefuzSetObj object for the set.
  89. //
  90. // Arguments:
  91. //
  92. //        int type - defuzzification method
  93. //
  94. // Returns:
  95. //
  96. //        0 - success
  97. //        non-zero - failure
  98. //
  99. // Author:    Michael Zarozinski
  100. // Date:    8/01
  101. // 
  102. // Modification History
  103. // Author    Date        Modification
  104. // ------    ----        ------------
  105. //
  106. //        
  107. int FuzzyOutSet::set_defuzz_method(int type)
  108. {
  109.     // free the previous object
  110.     if (defuzz_obj)
  111.         {
  112.         delete defuzz_obj;
  113.         defuzz_obj = NULL;
  114.         }
  115.  
  116.     // build teh correct type of defuzz object based on the method passed in
  117.     switch(type)
  118.         {
  119.         case DefuzzVarObj::DEFUZZ_TYPE::COG:
  120.  
  121.             // create COG memory
  122.             defuzz_obj = new COGDefuzzSetObj(this);
  123.             dynamic_cast<COGDefuzzSetObj*>(defuzz_obj)->init(FuzzyVariableBase::get_dom_array_count());
  124.  
  125.             break;
  126.  
  127.         case DefuzzVarObj::DEFUZZ_TYPE::MOM:
  128.  
  129.             // create COG memory
  130.             defuzz_obj = new MOMDefuzzSetObj(this);
  131.  
  132.             break;
  133.  
  134.         default:
  135.             set_msg_text(ERR_INVALID_DEFUZZ_MTHD);
  136.             return -1;
  137.  
  138.         } // end switch on type
  139.  
  140.     if (!defuzz_obj)
  141.         {
  142.         // problem allocating memory
  143.         // NOTE: in MSVC new returns NULL if there's not enough memory. If this is ported
  144.         // to a diff platform new may throw a std::bad_alloc exception it it can't alloc the memory.
  145.          set_msg_text(ERR_ALLOC_MEM);
  146.         assert(defuzz_obj != NULL);
  147.         return -1;
  148.         } // end if error allocating memory
  149.  
  150.     // call calc()
  151.     calc();
  152.  
  153.     return 0;
  154.  
  155. } // end FuzzyOutSet::set_defuzz_method()
  156.  
  157. //
  158. // Function:    calc()
  159. // 
  160. // Purpose:        Calculate the defuzzification look-up table (or
  161. //                whatever calc are needed for the specific defuzzification
  162. //                method) for this set.
  163. //
  164. // Arguments:
  165. //
  166. //        none
  167. //
  168. // Returns:
  169. //
  170. //        void
  171. //
  172. // Author:    Michael Zarozinski
  173. // Date:    8/01
  174. // 
  175. // Modification History
  176. // Author    Date        Modification
  177. // ------    ----        ------------
  178. //
  179. //        
  180. void FuzzyOutSet::calc()
  181. {
  182.     // if we don't have a member_func yet, nothing to calculate
  183.  
  184.     if (!member_func)
  185.         return;
  186.  
  187.     // call base version
  188.     FuzzySetBase::calc();
  189.  
  190.     // calc the defuzzifcation stuff
  191.     if (defuzz_obj)
  192.         defuzz_obj->calc();    
  193.  
  194. }; // end FuzzyOutSet::calc()
  195.  
  196. //
  197. // Function:    get_defuzz_x()
  198. // 
  199. // Purpose:        This fuction gets the defuzzified 'x' value for a DOM passed in.
  200. //                it's a generic function so the caller does not need to know the
  201. //                defuzzification method used.
  202. //
  203. // Arguments:
  204. //
  205. //        int        dom - 'y' value to get the 'x' value for (default: -1)
  206. //
  207. // Returns:
  208. //
  209. //        RealType - the defuzzified 'x' value for the set, FLT_MIN if something went wrong
  210. //
  211. // Author:    Michael Zarozinski
  212. // Date:    12/01
  213. // 
  214. // Modification History
  215. // Author    Date        Modification
  216. // ------    ----        ------------
  217. //
  218. //        
  219. RealType FuzzyOutSet::get_defuzz_x(int dom /* = -1 */)
  220. {
  221.  
  222.     // get the defuzzification method...
  223.  
  224.     FuzzyOutVariable* parent = get_parent();
  225.  
  226.     DefuzzSetObj* defuzz_base = get_defuzz_obj();
  227.  
  228.     int defuzz_type = defuzz_base->get_defuzz_type();
  229.  
  230.     if (defuzz_type == DefuzzVarObj::DEFUZZ_TYPE::COG && dom < 0)
  231.         return FLT_MIN; // invalid dom
  232.  
  233.     if (defuzz_type == DefuzzVarObj::DEFUZZ_TYPE::COG)
  234.         {
  235.         COGDefuzzSetObj* cog_defuzz = dynamic_cast<COGDefuzzSetObj*>(defuzz_base);
  236.         
  237.         return cog_defuzz->get_defuzz_x(dom); 
  238.  
  239.         } // end if CoG
  240.  
  241.     if (defuzz_type == DefuzzVarObj::DEFUZZ_TYPE::MOM)
  242.         {
  243.         MOMDefuzzSetObj* mom_defuzz = dynamic_cast<MOMDefuzzSetObj*>(defuzz_base);
  244.  
  245.         return mom_defuzz->get_mean_value(); 
  246.  
  247.         } // end if MoM
  248.  
  249.     // should never get her, but just in case...
  250.     return FLT_MIN;  
  251.  
  252. }; // end FuzzyOutSet::get_defuzz_x()
  253.         
  254.  
  255.  
  256. /////////////////////////////////////////////////////////////////////
  257. ////////// Trivial Functions That Don't Require Headers /////////////
  258. /////////////////////////////////////////////////////////////////////
  259.  
  260. DefuzzSetObj* FuzzyOutSet::get_defuzz_obj() const
  261.     return defuzz_obj; 
  262. };
  263.     
  264. FuzzyOutVariable* FuzzyOutSet::get_parent() const
  265. {
  266.     return dynamic_cast<FuzzyOutVariable*>(FuzzySetBase::get_parent());
  267. };
  268.